/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package lpsclient.model;

import entity.Customer;
import java.util.ArrayList;
import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;
/**
 *
 * @author DungTV
 */
public class DemoModel extends AbstractTableModel{
    private ArrayList<Customer> listCustomers ;
    private int currentPage;
    private int rowsPerPage =10;
    private int totalPage;

    private static final ColumnData columns[]  ={        
        new ColumnData("CustomerName",100,JLabel.LEFT),
        new ColumnData("IdentityCard",100,JLabel.LEFT),
        new ColumnData("Birthday",100,JLabel.LEFT),
        new ColumnData("Gender",100,JLabel.LEFT),
        new ColumnData("Telephone",100,JLabel.LEFT),
        new ColumnData("Address",100,JLabel.LEFT),
        new ColumnData("City",100,JLabel.LEFT),
        new ColumnData("Email",100,JLabel.LEFT),
        new ColumnData("Salary",100,JLabel.LEFT),
        new ColumnData("Status",100,JLabel.LEFT),
    };
 
   

    public DemoModel() {        
        this.listCustomers = new ArrayList<Customer>();
        this.setColumnData();
        this.currentPage = 1;
        this.totalPage =1;
    }

    

    public DemoModel(ArrayList<Customer> listCustomers) {
        this.listCustomers = listCustomers;
        this.setColumnData();
        if (this.listCustomers ==null) {
            this.listCustomers = new ArrayList<Customer>();
        }
        this.currentPage = 1;
        this.totalPage =this.listCustomers.size()/this.rowsPerPage+1;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
        this.fireModel();
    }




    public ArrayList<Customer> getListCustomers() {
        return listCustomers;
    }

    
    public void setListCustomers(ArrayList<Customer> listCustomers) {
        this.listCustomers = listCustomers;
        if (this.listCustomers == null){
            this.listCustomers = new ArrayList<Customer>();
        }
        this.totalPage =this.listCustomers.size()/this.rowsPerPage+1;
        fireModel();
    }

    

    public void setColumnData(){
//        if (this.customer.isAdmin()){
//            DemoModel.columns = DemoModel.columnsAdmin;
//        } else
//        DemoModel.columns = DemoModel.columnsCustomer;
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
//        if (customer.isAdmin()) return true;
//        else
            return false;

    }

    @Override
     public int getRowCount() {
        int rowCount = this.listCustomers.size();
        if (rowCount<this.rowsPerPage) return rowCount;
        else if (this.currentPage ==this.totalPage) return rowCount%this.rowsPerPage;
        else return this.rowsPerPage;         
    }

    @Override
    public int getColumnCount() {
        return columns.length;
    }

    @Override
    public String getColumnName(int column) {
        return columns[column].getTitle();
    }


    @Override
     public Object getValueAt(int rowIndex, int columnIndex) {
        Customer customer=listCustomers.get(rowIndex+(this.currentPage-1)*this.rowsPerPage);
        switch(columnIndex) {            
            case 0:
                return customer.getCustomerName();
            case 1:
                return customer.getIdentityCard();
            case 2:
                return customer.getBirthday();
            case 3:
                return customer.getGender();
            case 4:
                return customer.getTelephone();
            case 5:
                return customer.getAddress();
            case 6:
                return customer.getCity();
            case 7:
                return customer.getEmail();
            case 8:
                return customer.getSalary();
             case 9:
                return customer.isStatus();
        }
        return null;
    }

     @Override
    public void setValueAt(Object value, int nRow, int nCol) {
        Customer customer=listCustomers.get(nRow+(this.currentPage-1)*this.rowsPerPage);
        switch(nCol) {
             case 0:
                customer.setCustomerName((String) value);
                fireModel();
                break;
            case 1:
                customer.setIdentityCard((String) value);
                fireModel();
                break;
            case 2:
                customer.setBirthday((String) value);
                fireModel();
                break;
            case 3:
                customer.setGender((String) value);
                fireModel();
                break;
            case 4:
                customer.setTelephone((String) value);
                fireModel();
                break;
            case 5:
                customer.setAddress((String) value);
                fireModel();
                break;
            case 6:
                customer.setCity((String) value);
                fireModel();
                break;
            case 7:
                customer.setEmail((String) value);
                fireModel();
                break;
            case 8:
                customer.setSalary((Float) value);
                fireModel();
                break;
            case 9:
                customer.setStatus((Boolean) value);
                fireModel();
                break;
        }
        
    }

    public Customer getcustomerAt(int rowIndex){
            return this.listCustomers.get(rowIndex+(this.currentPage-1)*this.rowsPerPage);
        }
    public void RemoveRow(int rowIndex){
        this.listCustomers.remove(rowIndex);
        this.totalPage =this.listCustomers.size()/this.rowsPerPage+1;
        if (this.currentPage > this.totalPage) this.currentPage = this.totalPage;
        this.fireModel();
    }

    public void fireModel() {
        fireTableDataChanged();
    }

}
